package org.flexthinker.binding.bean.support
{
    import org.flexthinker.binding.value.support.MockBean;

    import org.hamcrest.assertThat;
    import org.hamcrest.core.throws;
    import org.hamcrest.object.equalTo;
    import org.hamcrest.object.instanceOf;

    /**
     * Dragos Dascalita Haut Nov 29, 2010
     */
    public class PropertyWriterTest
    {
        private var bean:MockBean;
        public function PropertyWriterTest()
        {
        }

        [Before]
        public function setUp():void
        {
            bean = new MockBean();
            bean.simpleString = "simpleString";
            bean.objectHolder = new MockBean();
            bean.objectHolder.simpleString = "substring";
        }

        [Test]
        public function testWriteOnSimplePath():void
        {
            PropertyWriter.setValue(bean, "simpleString", "newValueForSimpleString");
            assertThat("wrongValue has been written",bean.simpleString, equalTo("newValueForSimpleString"));
        }

        [Test]
        public function testWriteOnSubPaths():void
        {
            PropertyWriter.setValue(bean, "objectHolder.simpleString", "newValueForSubString");
            assertThat("wrong value has been written", bean.objectHolder.simpleString, equalTo("newValueForSubString"));
        }

        [Test]
        public function testWriteOnNullSubpaths():void
        {
            bean.objectHolder = null;
            assertThat( function():void
            {
                PropertyWriter.setValue(bean, "objectHolder.simpleString", "newValueForSubString");
            },throws(instanceOf(PropertyResolutionError)));
        }
    }
}